package com.hilaver.dzmis.service.impl;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.basicinfo.BiFranceCustomerVisitMemo;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class BiFranceCustomerServiceImpl extends AbstractBaseService implements
		IEntityService {

	public final static String COLUMN_FRANCE_CUSTOMER_ID = "fcId";
	
	public final static String COLUMN_BI_FRANCE_CUSTOMER_ID = "biFranceCustomer.id";
	
	@Override
	public String edit(Object obj) throws Exception {
		BiFranceCustomer custoer = (BiFranceCustomer)obj;
		String operation = null;
		if(custoer.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiFranceCustomer.class.getName(), id);
		return super.delete(BiFranceCustomer.class.getName(), id);
	}
	
	public String deleteVisitMemo(int id) throws Exception {
		return super.delete(BiFranceCustomerVisitMemo.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(BiFranceCustomer.class.getName(), id);
	}
	
	public String getVisitMemo(int id) throws Exception{
		BiFranceCustomerVisitMemo obj = (BiFranceCustomerVisitMemo)this.dao.get(BiFranceCustomerVisitMemo.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		
		xmlSB.append("<" + COLUMN_FRANCE_CUSTOMER_ID + ">");
		xmlSB.append(obj.getBiFranceCustomer().getId());
		xmlSB.append("</" + COLUMN_FRANCE_CUSTOMER_ID + ">");
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(BiFranceCustomer.class.getName(), page, sort, order);
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		return super.getAllPagination(BiFranceCustomer.class.getName(), page, sort, order, filters);
	}
	
	public String getAllVisitMemoPagination(int fcId, int page, String sort, String order, String[] filters)
		throws Exception {
		if(filters == null){
			filters = new String[1];
			filters[0] = COLUMN_BI_FRANCE_CUSTOMER_ID + "=" + fcId;
		}else{
			String[] temp = filters;
			filters = new String[temp.length + 1];
			filters[0] = COLUMN_BI_FRANCE_CUSTOMER_ID + "=" + fcId;
			for(int i = 1; i < filters.length; i ++){
				filters[i] = temp[i - 1];
			}
		}
		return super.getAllPagination(BiFranceCustomerVisitMemo.class.getName(), page, sort, order, filters);
	}
	
	public String getAll(String sort, String order) throws Exception{
		return super.getAll(BiFranceCustomer.class.getName(), sort, order);
	}
	
	public BiFranceCustomer getObj(int id) throws Exception {
		return (BiFranceCustomer)super.getObj(BiFranceCustomer.class.getName(), id);
	}

}
